import Dialog from '../../../vant-weapp/dist/dialog/dialog';
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //状态栏；
        status: [],
        queryString: {
            is_invoice: 0,
            page: 1,
        },
        //列表数据
        list: [],
        //选中ID
        result: [],
        dialogShow: false,
        popupShow: false,
        information_id: 0,
        information: [],
        //票据
        reviewShow:false,
        invoiceImg:''
    },

    reviewShowCancel(){
        this.setData({
            reviewShow : false
        })
    },

    detail(event) {
        let _this = this;
        app.loading();
        app.HttpsRequest('invoice/detail', 'GET', {
            order_id: event.currentTarget.dataset.id
        }).then(ret => {
            if(ret){
                wx:wx.hideLoading();
                _this.setData({
                    reviewShow: true,
                    invoiceImg:ret.data.invoice_path
                })
            }
        })
    },

    apply() {
        let _this = this;
        app.loading();
        app.HttpsRequest('invoice/create', 'POST', {
            information_id: _this.data.information_id,
            order_id: _this.data.result
        }).then(ret => {
            if (ret) {
                app.tips(ret.msg);
                setTimeout(function() {
                    _this.reset();
                    _this.setData({
                        list: []
                    })
                    _this.getInvoice();
                }, 1500)
            }
        })
    },

    onChangeRadio() {
        this.setData({
            information_id: event.detail
        });
    },
    onClickRadio(event) {
        const {
            name
        } = event.currentTarget.dataset;
        this.setData({
            information_id: name
        });
        console.log(this.data.information_id);
    },

    getInformation() {
        if (this.data.information.length) return;
        let _this = this;
        app.loading();
        app.HttpsRequest('buyer/index').then(ret => {
            if (ret) {
                wx.hideLoading();
                _this.setData({
                    information: ret.data
                })
            }
        })
    },

    /**
     * 确认开票
     */
    confirm() {
        if (this.data.result.length) {
            this.setData({
                dialogShow: !this.data.dialogShow
            })
        }
    },

    /**
     * 
     */
    showInformation() {
        this.setData({
            popupShow: !this.data.popupShow
        });
        this.getInformation();
    },

    /**
     * 重置选项
     */
    reset() {
        this.setData({
            result: [],
            dialogShow: false,
            popupShow: false,
            information_id: 0,
        });
    },

    /**
     * 切换选项卡
     */
    onChangeActive(e) {
        let _this = this;
        _this.setData({
            'queryString.is_invoice': e.detail.index,
            'queryString.page': 1,
            list: []
        })
        this.getInvoice(true);
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getInvoice();
    },



    /**
     * 获取开票列表；
     */
    getInvoice(active = false) {
        let _this = this;
        app.loading();
        app.HttpsRequest('Invoice/index', 'GET', _this.data.queryString).then(ret => {
            if (ret) {
                wx.hideLoading();
                if (active) {
                    _this.setData({
                        list: ret.data.list
                    })
                } else {
                    _this.setData({
                        list: _this.data.list.concat(ret.data.list),
                        status: ret.data.status
                    })
                }
            }
        })
    },
    onChange(event) {
        this.setData({
            result: event.detail
        });
        console.log(this.data.result)
    },

    toggle(event) {
        const {
            index
        } = event.currentTarget.dataset;
        const checkbox = this.selectComponent(`.checkboxes-${index}`);
        checkbox.toggle();
    },

    noop() {},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.setData({
            'queryString.page': this.data.queryString.page + 1
        });
        this.getInvoice();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})